package com.zhang.management;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zhang.bean.Course;
import com.zhang.bean.SC;

import zhang.jdbc.graph.Student;
import zhang.utils.JDBCUtils;

import javax.swing.JLabel;
import java.awt.Font;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class XueShengXuanKe extends JFrame {

	private JPanel contentPane;
	private JTextField txuefen;
	private JDBCUtils jdb = new JDBCUtils();
	private JLabel studentInfo;
	private JTextArea tintroduce;
	private Student dangQianXueSheng;
	private Course dangQianKeCheng;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					XueShengXuanKe frame = new XueShengXuanKe();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public  XueShengXuanKe(Student s) {
		this();
		studentInfo.setText("当前"+s.getname()+"在选课");
		dangQianXueSheng=s;
	}
	public XueShengXuanKe() {
		setDefaultCloseOperation(JFrame.TEXT_CURSOR);
		setBounds(100, 100, 548, 402);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		studentInfo = new JLabel("     学生选课");
		studentInfo.setFont(new Font("黑体", Font.PLAIN, 32));
		studentInfo.setBounds(113, 14, 261, 49);
		contentPane.add(studentInfo);
		
		JLabel lblNewLabel = new JLabel("可选课程：");
		lblNewLabel.setBounds(58, 73, 65, 15);
		contentPane.add(lblNewLabel);
		
		JComboBox suoYouKeCheng = new JComboBox();
		suoYouKeCheng.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				String name = suoYouKeCheng.getSelectedItem().toString();
				Course selectOne = jdb.selectOne(Course.class, name);
				txuefen.setText(selectOne.getGrade()+"");
				//初始化课程信息
				tintroduce.setText(selectOne.getIntroduce());
				//初始化当前课程
				dangQianKeCheng=selectOne;
			}
		});
		//初始话下拉列表
		Vector<String> v = new Vector<String>();
		List<Course> selectAll = jdb.selectAll(Course.class);
		Iterator<Course> it = selectAll.iterator();
		while(it.hasNext()) {
			v.add(it.next().getName());
		}
		
		suoYouKeCheng.setModel(new DefaultComboBoxModel(v));
		//
		
		suoYouKeCheng.setBounds(133, 69, 105, 23);
		contentPane.add(suoYouKeCheng);
		
		JLabel lblNewLabel_1 = new JLabel("课程介绍");
		lblNewLabel_1.setBounds(58, 132, 58, 15);
		contentPane.add(lblNewLabel_1);
		
		txuefen = new JTextField();
		txuefen.setEnabled(false);
		txuefen.setBounds(386, 70, 66, 21);
		contentPane.add(txuefen);
		txuefen.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("学分：");
		lblNewLabel_2.setBounds(318, 73, 58, 15);
		contentPane.add(lblNewLabel_2);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(84, 157, 404, 123);
		contentPane.add(scrollPane);
		
		tintroduce = new JTextArea();
		scrollPane.setViewportView(tintroduce);
		
		JButton btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SC sc = new SC(dangQianXueSheng.getSid(), dangQianKeCheng.getid(), 0, 0, 0, "");
				jdb.insert(sc);
			}
		});
		btnNewButton.setBounds(126, 308, 97, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SC sc = new SC(dangQianXueSheng.getSid(), dangQianKeCheng.getid(), 0, 0, 0, "");
				jdb.deleteSC(sc);
			}
		});
		btnNewButton_1.setBounds(279, 308, 97, 23);
		contentPane.add(btnNewButton_1);
		//初始化学分框
		String name = suoYouKeCheng.getSelectedItem().toString();
		Course selectOne = jdb.selectOne(Course.class, name);
		txuefen.setText(selectOne.getGrade()+"");
		//初始化课程信息
		tintroduce.setText(selectOne.getIntroduce());
		//初始话当前学生
		dangQianKeCheng=selectOne;
		this.setVisible(true);
	}
}
